/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function(){
    if (location.href != config.domain+'/login.html') {
        if (!_token && !$.cookie()._token) {
            location.href = config.domain+'/login.html';
        }
        if (_token && $.cookie()._token != _token) {
            $.cookie('_token', _token, {expires: 1, path: '/'});
        }
        if (role && $.cookie().role != role) {
            $.cookie('role', role, {expires: 1, path: '/'});
        }
        var authorization = $.cookie()._token;
        if (!authorization) {
            location.href = config.domain+'/login.html';
        }
        if (location.href != config.domain+'/login.html') {
            sendAjax('/v1/check.json', 'get', {}, function(json){
                if (json.status == 4031) {
                    swal({
                        title: "糟糕",
                        text: json.message,
                        type: "error"
                    }, function (isConfirm) {
                        if (isConfirm) {
                            location.href = config.domain+'/login.html';
                        }
                    });
                }

                if ($('.log-out').attr('href')) {
                    var purl = $.url($('.log-out').attr('href'));
                    if (purl.param('_token') == '') {
                        var url = purl.attr('path')+'?_token='+$.cookie()._token;
                        if (!$.cookie().role && $.cookie().role == '') {
                            $.cookie('role', role, {expires: 1, path: '/'});
                        }
                        url += '&role='+$.cookie().role;
                        $('.log-out').attr('href', url);
                    }

                }
            });
        }
    } else {
        $.cookie('_token', null, {expires: 1, path: '/'});
        $.cookie('role', null, {expires: 1, path: '/'});
        $.cookie('PHPSESSID', null, {expires: 1, path: '/'});
    }
});
function sendAjax(url, method, data, callbakck) {
    $.ajax({
        type: method,
        url: url,
        data: data,
        headers: {
            Authorization: $.cookie()._token,
            Accept: "application/json; charset=utf-8"
        },
        success: function(e) {
            var json = JSON.parse(e);
            callbakck(json);
        }
    });
}

function saveData(url, data, redirect, text) {
    sendAjax(url, 'post', data, function(json){
        if (json.status == 200) {
            swal({
                title:"太帅了",
                text:text ? text : "小手一抖又成功添加一条数据",
                type:"success"
            }, function(isConfirm){
                if (isConfirm) {
                    redirect == '' ? location.reload() : (location.href = redirect);
                }
            });
        } else {
            swal({
                title:"糟糕",
                text:json.message,
                type:"error"
            })
        }
    });
}

function deleteData(url, id) {
    swal({
        title:"您确定要删除这条信息吗",
        text:"删除后将无法恢复，请谨慎操作！",
        type:"warning",
        showCancelButton:true,
        confirmButtonColor:"#DD6B55",
        confirmButtonText:"是的，我要删除！",
        cancelButtonText:"我再考虑一下…",
        closeOnConfirm:false,
        closeOnCancel:false
    },function(isConfirm){
        if(isConfirm){
            sendAjax(url, 'post', {id:id}, function(json){
                if (json.status == 200) {
                    swal({
                        title:"删除成功！",
                        text:"您已经永久删除了这条信息。",
                        type:"success"
                    }, function(isConfirm){
                        if (isConfirm) {
                            location.reload();
                        }
                    });
                } else {
                    swal("糟糕",json.message,"error")
                }
            })
        }else{
            swal("已取消","您取消了删除操作！","error")
        }
    });
}

function alertTip(tip) {
    var html = '<div class="alert alert-danger alert-dismissible">';
    html += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>';
    html += '<h4><i class="icon fa fa-ban"></i> Alert!&nbsp;<strong class="tip-content" style="color:gold;">' + tip + '</strong></h4>';
    html += '</div>';
    $('.box-body').prepend(html);
}

function pageList(page, page_size, total, pages, length) {
    var html = '<div class="col-sm-5">';
    var start = (page - 1) * page_size + 1;
    var end = start + length -1;
    html += '<div class="dataTables_info" id="example1_info" role="status" aria-live="polite">显示第' + start + ' 至 ' + end + ' 条,共 ' + total + ' 条</div></div>';
    html += '<div class="col-sm-7"><div class="dataTables_paginate paging_simple_numbers text-right" id="example1_paginate">';
    html += '<ul class="pagination">';
    html += '<li class="paginate_button previous' + (page > 1 ? '' :' disabled') + '" id="example1_previous"><a href="#" aria-controls="example1" data-id="' + (page - 1) + '" tabindex="0">上一页</a></li>';
    for (var i = 1; i <= pages; i ++) {
        // if (pages > 10) {
        //     if (page <= 4 || page > pages -4) {
        //         if (i > 5 && i < pages - 4) {
        //             if(i == 6) page_list += '<li class="paginate_button"><a>...</a></li>';
        //             continue;
        //         }
        //     } else if (page <= 6) {
        //         if (i > (page + 1) && i < (pages - 8 + page)) {
        //             if (i == page + 1) page_list += '<li class="paginate_button"><a>...</a></li>';
        //             continue;
        //         }
        //     } else if (page > 6 && page < pages - 5) {
        //         if ((i > 3 && i < page -1) || (i > page + 1 && i < pages - 2)) {
        //             if (i == page - 2 || i == page + 2) page_list += '<li class="paginate_button"><a>...</a></li>';
        //             continue;
        //         }
        //     } else {
        //         if (i > (page + 8 - pages) && i < (pages - page + 1)) {
        //             if (i = page + 9 - pages) page_list += '<li class="paginate_button"><a>...</a></li>';
        //             continue;
        //         }
        //     }
        // }
        html += '<li class="paginate_button' + (page == i ? ' active' : '') + '"><a href="#" aria-controls="example1" data-id="' + i + '" tabindex="0">' + i + '</a></li>';
    }
    html += '<li class="paginate_button next'+ (pages > page ? '' : ' disabled') + '" id="example1_next"><a href="#" aria-controls="example1" data-id="' + (page + 1) + '" tabindex="0">下一页</a></li>';
    html += '</ul></div></div>';
    return html;
}



function getLevelName(level) {
    switch (level) {
        case 1: return '不合格';
        case 2: return '基本合格';
        case 3: return '合格';
        case 4: return '优秀';
        default: return '';
    }
}